﻿Imports System.Text
Imports System.Data.OracleClient
Imports DatabaseAccess.DBUtility
Imports System.Xml
Imports BusinessRule.DTO
Imports BusinessRule.IBusiness

Public Class uLMSUserLoginBusiness
    Implements IBusiness.uLMSUserLoginIBusiness
    ''' <summary>
    ''' 添加登录人信息（ip）
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function AddUserLoginInfo(ByVal para As Dictionary(Of String, String)) As Boolean Implements IBusiness.uLMSUserLoginIBusiness.AddUserLoginInfo
        Dim strsql As New StringBuilder
        strsql.AppendLine("INSERT INTO USERLOGININFO(EMPLOYEENAME,LOGINIP,LOGINDATE,USERLOGININFO,NOTE,IsCard) VALUES(:EMPLOYEENAME,:LOGINIP,SYSDATE,:USERLOGININFO,:NOTE,:IsCard)")
        Dim insertParam(4) As OracleParameter
        insertParam(0) = DatabaseHelper.NewParameter(":EMPLOYEENAME", para("EmployeeName"), OracleType.VarChar)
        insertParam(1) = DatabaseHelper.NewParameter(":LOGINIP", para("IP"), OracleType.VarChar)
        insertParam(2) = DatabaseHelper.NewParameter(":USERLOGININFO", System.Guid.NewGuid.ToString(), OracleType.VarChar)
        insertParam(3) = DatabaseHelper.NewParameter(":NOTE", DBNull.Value, OracleType.VarChar) '暂时为空
        insertParam(4) = DatabaseHelper.NewParameter(":IsCard", para("IsCard"), OracleType.VarChar)
        If DatabaseHelper.ExecuteSql(strsql.ToString(), insertParam) > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' create by Yagnsj 2018-11-19 
    ''' 根据菜单获取域账号
    ''' </summary>
    ''' <param name="MenuName"></param>
    ''' <param name="Usercount"></param>
    ''' <param name="UserName"></param>
    ''' <param name="Password"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetDomainUser(ByVal MenuName As String, ByVal Usercount As Dictionary(Of String, Integer), ByRef UserName As String, ByRef Password As String) As Boolean Implements uLMSUserLoginIBusiness.GetDomainUser
        Dim Doc As XmlDocument = New XmlDocument()
        Doc.Load(System.AppDomain.CurrentDomain.BaseDirectory + "\UserAssociate.xml")
        UserName = String.Empty
        Password = String.Empty
        For Each elem As XmlElement In Doc.SelectSingleNode("/Root/Users").ChildNodes
            If elem.Name <> "User" Then
                Continue For
            End If
            If elem.GetAttribute("MenuName") = MenuName Then
                If Usercount.ContainsKey(elem.GetAttribute("UserName")) Then
                    '用户已登录次数判断
                    If Usercount(elem.GetAttribute("UserName")) > Configuration.ConfigurationManager.AppSettings("UserCount") Then
                        Continue For
                    End If
                End If

                UserName = elem.GetAttribute("UserName")
                Password = elem.GetAttribute("Password")
                Exit For
            End If
        Next
        If String.IsNullOrWhiteSpace(UserName) Then
            GetDomainUser = False
        Else
            GetDomainUser = True
        End If
    End Function
    ''' <summary>
    '''create by Yagnsj 2018-11-19 
    ''' 根据分厂获取域账号
    ''' </summary>
    ''' <param name="FactoryName"></param>
    ''' <param name="UserName"></param>
    ''' <param name="Password"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetDomainUser(ByVal FactoryName As String, ByRef UserName As String, ByRef Password As String) As Boolean Implements uLMSUserLoginIBusiness.GetDomainUser
        Dim Doc As XmlDocument = New XmlDocument()
        Doc.Load(System.AppDomain.CurrentDomain.BaseDirectory + "\UserAssociate.xml")
        UserName = String.Empty
        Password = String.Empty
        For Each elem As XmlElement In Doc.SelectSingleNode("/Root/FactoryUsers").ChildNodes
            If elem.Name <> "User" Then
                Continue For
            End If
            If elem.GetAttribute("FactoryName") = FactoryName Then
                UserName = elem.GetAttribute("UserName")
                Password = elem.GetAttribute("Password")
                Exit For
            End If
        Next
        If String.IsNullOrWhiteSpace(UserName) Then
            GetDomainUser = False
        Else
            GetDomainUser = True
        End If
    End Function

    ''' <summary>
    ''' create by Yagnsj 2018-11-19 
    ''' 根据用户获取用户信息
    ''' </summary>
    ''' <param name="EmployeeName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Ajax.AjaxMethod()> Public Function GetEmployeeByName(ByVal EmployeeName As String) As uMES_AjaxEmployeeDto Implements uLMSUserLoginIBusiness.GetEmployeeByName

        Dim sql As String = <string>SELECT E.*, M.MENUDEFINITIONNAME,MI.PARENTID  FROM EMPLOYEE E
                LEFT JOIN MENUDEFINITION M ON M.MENUDEFINITIONID = E.WEBMENUDEFINITIONID  
                LEFT JOIN (SELECT PARENTID FROM MENUITEM WHERE WORKINGSECTIONURL = 'WorkingSectionMaint.aspx'  GROUP BY PARENTID) MI
                ON MI.PARENTID = E.WEBMENUDEFINITIONID
                    </string>
        sql += String.Format("  where lower(e.employeename)='{0}'", EmployeeName)
        Dim dt As DataTable = DatabaseHelper.Query_ReturnDatTable(sql)
        Dim dto As uMES_AjaxEmployeeDto = New uMES_AjaxEmployeeDto()

        If dt.Rows.Count > 0 Then
            dto.strEmployeeID = dt.Rows(0)("employeeid").ToString()
            dto.strEmployeeName = dt.Rows(0)("employeename").ToString()
            dto.strCardNum = dt.Rows(0)("cardnum").ToString()
            dto.strPassword = dt.Rows(0)("password").ToString()
            dto.DomainName = dt.Rows(0)("domainname").ToString()
            dto.MenuName = dt.Rows(0)("menudefinitionname").ToString()
            If (dt.Rows(0).IsNull("DomainLogin")) Then
                dto.bolDomainLogin = False
            Else : dto.bolDomainLogin = Convert.ToBoolean(dt.Rows(0)("DomainLogin"))
            End If

            If (dt.Rows(0).IsNull("cardLogin")) Then
                dto.bolCardLogin = False
            Else : dto.bolCardLogin = Convert.ToBoolean(dt.Rows(0)("cardLogin"))
            End If

            If (dt.Rows(0).IsNull("PARENTID")) Then
                dto.HasMaintMenu = False
            Else
                dto.HasMaintMenu = True
            End If

        End If
        Return dto
    End Function

    ''' <summary>
    ''' create by Yagnsj 2018-11-19 
    ''' 根据用户获取用户信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Ajax.AjaxMethod()> Public Function GetEmployeeByCardNum(ByVal CardNum As String) As uMES_AjaxEmployeeDto Implements uLMSUserLoginIBusiness.GetEmployeeByCardNum
        Dim sql As String = <string>SELECT E.*, M.MENUDEFINITIONNAME,MI.PARENTID  FROM EMPLOYEE E
                LEFT JOIN MENUDEFINITION M ON M.MENUDEFINITIONID = E.WEBMENUDEFINITIONID  
                LEFT JOIN (SELECT PARENTID FROM MENUITEM WHERE WORKINGSECTIONURL = 'WorkingSectionMaint.aspx'  GROUP BY PARENTID) MI
                ON MI.PARENTID = E.WEBMENUDEFINITIONID
            </string>

        sql += String.Format("  where e.cardnum='{0}'", CardNum)
        Dim dt As DataTable = DatabaseHelper.Query_ReturnDatTable(sql)
        Dim dto As uMES_AjaxEmployeeDto = New uMES_AjaxEmployeeDto()

        If dt.Rows.Count > 0 Then
            dto.strEmployeeName = dt.Rows(0)("employeename").ToString()
            dto.strFullName = dt.Rows(0)("FullName").ToString
            dto.strCardNum = dt.Rows(0)("cardnum").ToString()
            dto.strPassword = dt.Rows(0)("password").ToString()
            dto.MenuName = dt.Rows(0)("menudefinitionname").ToString()
            dto.DomainName = dt.Rows(0)("domainname").ToString()
            dto.strEmployeeID = dt.Rows(0)("employeeid").ToString() 'add tianFW 2017年12月7日
            If (dt.Rows(0).IsNull("DomainLogin")) Then
                dto.bolDomainLogin = False
            Else : dto.bolDomainLogin = Convert.ToBoolean(dt.Rows(0)("DomainLogin"))
            End If

            If (dt.Rows(0).IsNull("cardLogin")) Then
                dto.bolCardLogin = False
            Else : dto.bolCardLogin = Convert.ToBoolean(dt.Rows(0)("cardLogin"))
            End If

            If (dt.Rows(0).IsNull("PARENTID")) Then
                dto.HasMaintMenu = False
            Else
                dto.HasMaintMenu = True
            End If

        End If
        Return dto
    End Function

End Class
